#!/usr/bin/env bash
# redeploy.sh
# 下载模块最新代码重新构建、部署、运行

set -e 

##################### 获取工作目录 #####################
# 参考：http://www.gimoo.net/t/1502/54ea928e14fef.html
SOURCE="$0"
while [ -h "$SOURCE"  ]; do                              # resolve $SOURCE until the file is no longer a symlink
    DIR="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"
    SOURCE="$(readlink "$SOURCE")"
    [[ $SOURCE != /*  ]] && SOURCE="$DIR/$SOURCE"        # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE"  )" && cd .. && pwd  )"
#######################################################

# app=$(basename "$DIR")
workdir=$DIR  # /opt/ems

# 参数：<模块>，可能的值：
#   固定为 archive
module='archive'

# 使用说明
usage() {
    echo -e "-------------------------------------------------------"
    echo "HELP: 用法: ./redeploy.sh"
    echo "-------------------------------------------------------"
    exit 1
}



# 函数：部署后端模块
redeploy_server_module(){
  echo "INFO: [$module] 重新部署"
  
  echo "INFO: [$module] ========================================================== 构建 =========================================================="
  $workdir/bin/build.sh

  echo "INFO: [$module] ========================================================== 部署 =========================================================="
  $workdir/bin/deploy.sh

  echo "INFO: [$module] ========================================================== 重启 =========================================================="
  $workdir/bin/control.sh restart -p prod
}


# 程序入口
case "$module" in
  "archive")
    redeploy_server_module
  ;;
  *)
    echo "ERR : [$module] 程序入口无法匹配<模块>"
    usage
  ;;
esac

